﻿<%@ WebHandler Language="C#"  Class="ReportTracking.Services.Report.PL3" %>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DocumentFormat.OpenXml;
using SpreadsheetLight;
using SpreadsheetLight.Drawing;
using System.Text;
using DocumentFormat.OpenXml.Wordprocessing;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Data;

namespace ReportTracking.Services.Report
{
    /// <summary>
    /// Summary description for PL3
    /// </summary>
    public class PL3 : IHttpHandler
    {
        string sPath = "";
        public void ProcessRequest(HttpContext context)
        {
            sPath = HttpContext.Current.Request.PhysicalApplicationPath;
            context.Response.ContentType = "text/plain";
            int nIndexRow = 0;
            int nIndexColumn = 0;
            int nColumn = 14;
            string charEnd = "N";
            InfoReport inforeport = (InfoReport)context.Session["InfoReprot"];
            SLDocument sl = new SLDocument();
            System.Drawing.Bitmap bm = new System.Drawing.Bitmap(sPath + "Images/logo/VBDlogo.png");
            byte[] ba;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                bm.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                ms.Close();
                ba = ms.ToArray();
            }
            SLPicture pic = new SLPicture(ba, DocumentFormat.OpenXml.Packaging.ImagePartType.Png);
            pic.SetPosition(nIndexRow, nIndexColumn);
            sl.InsertPicture(pic);

            SLStyle style = sl.CreateStyle();
            style.Alignment.Horizontal = DocumentFormat.OpenXml.Spreadsheet.HorizontalAlignmentValues.Justify;
            style.Font.FontName = "Arial";
            style.Font.FontSize = 30;
            style.Font.FontColor = System.Drawing.Color.Blue;
            style.Font.Bold = true;
            sl.SetCellValue("C1", "Thống kê vi phạm về hành trình chạy xe");
            sl.SetCellStyle("C1", style);
            sl.MergeWorksheetCells("C1", "N4");

            style.Font.FontName = "Arial";
            style.Font.FontSize = 12;
            style.Font.FontColor = System.Drawing.Color.Crimson;
            style.Font.Bold = true;

            if (inforeport != null)
            {
                StringBuilder sHD = new StringBuilder();
                sHD.Append("Thống kê vi phạm về hành trình chạy xe ");
                sHD.Append(". Từ ngày: ");
                sHD.Append(inforeport.FromDay);
                sHD.Append(" đến ");
                sHD.Append(inforeport.ToDay);
                nIndexRow = nIndexRow + 6;
                nIndexColumn++;
                sl.MergeWorksheetCells("A6", charEnd + "6");
                sl.SetCellValue(nIndexRow, nIndexColumn, sHD.ToString());
                sl.SetCellStyle(nIndexRow, nIndexColumn, style);
                nIndexRow++;
                sHD = new StringBuilder();
                style.Font.FontColor = System.Drawing.Color.MediumBlue;
                sHD.Append("Đơn vị kinh doanh vận tải:");
                if (inforeport.DVKD.Length > 0)
                {
                    for (int i = 0; i < inforeport.DVKD.Length; i++)
                    {
                        sHD.Append(inforeport.DVKD[i]);
                        sHD.Append("");
                    }

                }
                sl.MergeWorksheetCells("A7", charEnd + "7");
                sl.SetCellValue(nIndexRow, nIndexColumn, sHD.ToString());
                sl.SetCellStyle(nIndexRow, nIndexColumn, style);
            }
            nIndexRow++;

            style.SetWrapText(true);
            
            style.Font.FontSize = 10;
            style.Font.FontColor = System.Drawing.Color.SlateBlue;

            style.Border.LeftBorder.BorderStyle = BorderStyleValues.Thin;
            style.Border.LeftBorder.Color = System.Drawing.Color.YellowGreen;
            style.Border.TopBorder.BorderStyle = BorderStyleValues.Thin; 
            style.Border.TopBorder.Color = System.Drawing.Color.YellowGreen;
            style.Border.RightBorder.BorderStyle = BorderStyleValues.Thin;
            style.Border.RightBorder.Color = System.Drawing.Color.YellowGreen;
            style.Border.BottomBorder.BorderStyle = BorderStyleValues.Thin; 
            style.Border.BottomBorder.Color = System.Drawing.Color.YellowGreen;

            sl.SetCellStyle(nIndexRow, nIndexColumn, (nIndexRow + 1), nColumn,style);
            sl.MergeWorksheetCells("A" + nIndexRow, "A" + (nIndexRow+1));
            sl.SetCellValue(nIndexRow, nIndexColumn, "STT");
            

            sl.MergeWorksheetCells("B" + nIndexRow, "B" + (nIndexRow +1));
            sl.SetCellValue(nIndexRow, (nIndexColumn+1), "Biển kiểm soát");

            sl.MergeWorksheetCells("C" + nIndexRow, "C" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 2), "Đơn vị kinh doanh");

            sl.MergeWorksheetCells("D" + nIndexRow, "D" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 3), "Đơn vị quản lý");

            sl.MergeWorksheetCells("E" + nIndexRow, "E" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 4), "Họ tên lái xe");

            sl.MergeWorksheetCells("F" + nIndexRow, "F" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 5), "Số giấy phép lái xe");

            sl.MergeWorksheetCells("G" + nIndexRow, "G" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 6), "Loại hình hoạt động");

            sl.MergeWorksheetCells("H" + nIndexRow, "H" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 7), "Ngày hoạt động");

            sl.MergeWorksheetCells("I" + nIndexRow, "I" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 8), "Tổng km hoạt động trong ngày");

            sl.MergeWorksheetCells("J" + nIndexRow, "L" + nIndexRow);
            sl.SetCellValue("J" + nIndexRow, "Quãng đường xe chạy sai hành trình");
            sl.AutoFitRow(7, 100);
            nIndexRow++; //+ nIndexRow =9
            sl.SetCellValue("J" + nIndexRow, "Tổng số km chạy sai hành trình(km)");
            sl.SetCellValue("K" + nIndexRow, "Điểm bắt đầu");
            sl.SetCellValue("L" + nIndexRow, "Điểm kết thúc");
            nIndexRow--;//nIndexRow = 8
            sl.MergeWorksheetCells("M" + nIndexRow, "M" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + 12), "Ghi chú chưa rõ");

            sl.MergeWorksheetCells("N" + nIndexRow, "N" + (nIndexRow + 1));
            sl.SetCellValue(nIndexRow, (nIndexColumn + nColumn - 1), "Ghi chú");

            style.Font.FontSize = 10;
            style.Font.FontColor = System.Drawing.Color.SlateBlue;
            style.Font.Bold = false;

            nIndexRow = nIndexRow +2;
            DataTable tb = (DataTable)context.Session["Data"];
           if (tb.Rows.Count > 0)
            {
                int nTotal = tb.Rows.Count;
                for (int r = 0; r < nTotal; r++)
                {
                    sl.SetCellValue(nIndexRow, 1, (r + nTotal));
                    for (int c = 1; c < nColumn; c++)
                    {
                        sl.SetCellValue(nIndexRow, c + 1, r + c);
                        sl.SetCellStyle(nIndexRow, c , style);
                        sl.SetCellStyle(nIndexRow, nColumn, style);
                        //sl.AutoFitColumn(nIndexRow, c);
                    }
                    
                    nIndexRow++;
                }
                sl.AutoFitRow(7, nTotal);
            }
            
            string sFileName = Guid.NewGuid().ToString();
            sl.SaveAs(sPath + "FileExcel/" + sFileName + ".xlsx");
            Utilities.DeleteFile(sPath + "FileExcel/", sFileName);
            context.Response.Write(sFileName);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}